package cn.congzhou.crm.service.impl;

import cn.congzhou.crm.common.qo.QueryObject;
import cn.congzhou.crm.common.vo.PageTableData;
import cn.congzhou.crm.domain.Department;
import cn.congzhou.crm.mapper.DepartmentMapper;
import cn.congzhou.crm.service.IDepartmentService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 高级查询部门信息
     * @param qo
     * @return
     */
    @Override
    public PageTableData<Department> queryDepartment(QueryObject qo) {
        Page page  = PageHelper.startPage(qo.getPage(), qo.getLimit());
        List<Department> departments = departmentMapper.queryDepartment(qo);
        long total = page.getTotal();
        PageTableData<Department> pageTableData = new PageTableData<>(total,departments);
        return pageTableData;
    }

    @Override
    public void deleteById(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(Department department) {
        Long id = department.getId();
        if ( id== null){
            departmentMapper.insert(department);
        }else{
            departmentMapper.updateByPrimaryKey(department);
        }
    }
}
